import 'package:flutter/foundation.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/core/utils/show_toast.dart';

import '../../../data/controllers/cp_controller.dart';
import '../../../data/controllers/links_controller.dart';
import '../../links_page/models/link_model.dart';
import '../models/public_home_data.dart';

part 'public_home_state.dart';

class PublicHomeCubit extends Cubit<PublicHomeState> {
  PublicHomeCubit() : super(PublicHomeInitial());

  static PublicHomeCubit get(context) => BlocProvider.of(context);
  late PublicHomeData publicHomeData ;
  void init() {
    emit(PublicHomeDone());
    //getAllLinks();
    //getHomeData();
  }

  getHomeData()async{
    emit(PublicHomeLoading());

    await CPController.getPublicHomeData().then((value){
      if(value == null){
        debugPrint('home controller return null!!');
        showErrorMessage(message: 'حدث خطأ الرجاء إعادة المحاولة لاحقاً');
        emit(PublicHomeError());
        return ;
      }
      publicHomeData =PublicHomeData.fromJson(value.data);
      emit(PublicHomeDone());
    }).catchError((e){
      showErrorMessage(message: 'حدث خطأ الرجاء إعادة المحاولة لاحقاً');
      emit(PublicHomeError());
    });
  }

  List<LinkModel> links = [
    LinkModel(
        link: AppConstant.facebookLink,
        title: 'فيسبوك',
        icon: ImageConstant.icFacebook,
        linkId: '1',
        createAt: DateTime.now(),
        updateAt: DateTime.now(),
        createdBy: '1',
        active: '1',
    ),
    LinkModel(
      link: AppConstant.whatsappLink,
      title: 'واتس اب',
      icon: ImageConstant.icWhatsapp,
      linkId: '2',
      createAt: DateTime.now(),
      updateAt: DateTime.now(),
      createdBy: '1',
      active: '1',
    ),
    LinkModel(
      link: AppConstant.instagramLink,
      title: 'انستغرام',
      icon: ImageConstant.icInstagram,
      linkId: '3',
      createAt: DateTime.now(),
      updateAt: DateTime.now(),
      createdBy: '1',
      active: '1',
    ),
    /*LinkModel(
      link: AppConstant.youtubeLink,
      title: 'يوتيوب',
      icon: ImageConstant.icYoutube,
      linkId: '4',
      createAt: DateTime.now(),
      updateAt: DateTime.now(),
      createdBy: '1',
      active: '1',
    ),*/


  ];

  Future getAllLinks()async{
    emit(PublicHomeLoading());

    await LinksController.getAllLinks(isPublic: true).catchError((e){
      emit(PublicHomeError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        links = List<LinkModel>.generate(value.data.length, (index) => LinkModel.fromJson(value.data[index]));
        emit(PublicHomeDone());
      }else if(value != null){
        showMessage(value.message);
        emit(PublicHomeError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }



}
